package com.changpei.core.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.changpei.common.result.Result;
import com.changpei.core.pojo.Comment;
import com.changpei.core.service.CommentService;
import com.changpei.core.utils.SearchUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>Title : Comment</p>
 * <p>Description : comment的控制层</p>
 *
 * @author weibiao
 * @date 2020年3月7日
 */

@RestController
@RequestMapping("/comment")
@CrossOrigin
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * <p>Title : add</p>
     * <p>Description : 新增Comment数据</p>
     *
     * @param comment
     * @return
     */
    @PostMapping(value = "/add")
    public Result<Map<String, Object>> add(@RequestBody Map<String, Object> map) {
        return Result.ok(commentService.comment(map));
    }

    /**
     * <p>Title : reply</p>
     * <p>Description : 回复Comment数据</p>
     *
     * @param comment
     * @return
     */
    @PostMapping(value = "/reply")
    public Result<Map<String, Object>> reply(@RequestBody Map<String, Object> map) {
        return Result.ok(commentService.comment(map));
    }

    /**
     * <p>Title : delete</p>
     * <p>Description : 删除评论</p>
     *
     * @param comment
     * @return
     */
    @PostMapping(value = "/delete")
    public Result<Boolean> delete(@RequestBody Map<String, Object> map) {
        if (commentService.delete(map)) {
            return Result.ok(true);
        }
        return Result.failed();
    }

    /**
     * <p>Title : like</p>
     * <p>Description : 评论点赞</p>
     *
     * @param comment
     * @return
     */
    @PostMapping(value = "/like")
    public Result<JSONObject> like(@RequestBody Map<String, Object> map) {
        return Result.ok(commentService.like(map));
    }

    @PostMapping(value = "/unlike")
    public Result<JSONObject> unlike(@RequestBody Map<String, Object> map) {
        return Result.ok(commentService.unlike(map));
    }

    /**
     * <p>Title : delete</p>
     * <p>Description : Comment根据ID数据</p>
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public Result<String> delete(@PathVariable Long id) {
        // 调用CommentService实现根据主键删除
        if (commentService.removeById(id)) {
            return Result.ok();
        }
        return Result.failed();
    }

    /**
     * <p>Title : update</p>
     * <p>Description : Comment根据ID修改</p>
     *
     * @param comment
     * @param id
     * @return
     */
    @PutMapping(value = "/{id}")
    public Result<String> update(@RequestBody Comment comment, @PathVariable Long id) {
        // 设置主键值
        comment.setId(id);
        // 调用CommentService实现修改Comment
        if (commentService.updateById(comment)) {
            return Result.ok();
        }
        return Result.failed();
    }

    /**
     * <p>Title : findPage</p>
     * <p>Description : Comment分页搜索实现</p>
     *
     * @param pageNum  : 当前页
     * @param pageSize : 每页显示多少条
     * @return
     */
    @GetMapping(value = "/search/{pageNum}/{pageSize}")
    public Result<Page<Comment>> findPage(@PathVariable int pageNum, @PathVariable int pageSize) {
        // 调用CommentService实现分页查询Comment
        Page<Comment> pageInfo = commentService.page(new Page<>(pageNum, pageSize));
        return Result.ok(pageInfo);
    }

    /**
     * <p>Title : findPage</p>
     * <p>Description : Comment分页条件搜索实现</p>
     *
     * @param comment
     * @param pageNum  : 当前页
     * @param pageSize : 每页显示多少条
     * @return
     */
    @PostMapping(value = "/search/{pageNum}/{pageSize}")
    public Result<Page<Comment>> findPage(@RequestBody(required = false) Comment comment, @PathVariable int pageNum, @PathVariable int pageSize) {
        // 调用CommentService实现分页条件查询Comment
        Page<Comment> pageInfo = commentService.page(new Page<>(pageNum, pageSize), SearchUtil.initQuery(comment));
        return Result.ok(pageInfo);
    }

    /**
     * <p>Title : findList</p>
     * <p>Description : 多条件搜索Comment数据</p>
     *
     * @param comment
     * @return
     */
    @PostMapping(value = "/search")
    public Result<List<Comment>> findList(@RequestBody(required = false) Comment comment) {
        // 调用CommentService实现条件查询Comment
        List<Comment> list = commentService.list(SearchUtil.initQuery(comment));
        return Result.ok(list);
    }

    /**
     * <p>Title : findById</p>
     * <p>Description : 根据ID查询Comment数据</p>
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/{id}")
    public Result<Comment> findById(@PathVariable Long id) {
        // 调用CommentService实现根据主键查询Comment
        Comment comment = commentService.getById(id);
        return Result.ok(comment);
    }

    /**
     * <p>Title : findOne</p>
     * <p>Description : 多条件搜索Comment数据，结果返回一条</p>
     *
     * @param comment
     * @return
     */
    @PostMapping(value = "/findOne")
    public Result<Comment> findOne(@RequestBody(required = false) Comment comment) {
        // 调用CommentService实现条件查询Comment
        Comment commentResult = commentService.getOne(SearchUtil.initQuery(comment));
        return Result.ok(commentResult);
    }

    /**
     * <p>Title : findAll</p>
     * <p>Description : 查询Comment全部数据</p>
     *
     * @return
     */
    @GetMapping(value = "/all")
    public Result<List<Comment>> findAll() {
        // 调用CommentService实现查询所有Comment
        List<Comment> list = commentService.list();
        return Result.ok(list);
    }

}
